<?php

namespace App\Admin\Controllers;

use App\Models\Project;
use App\Models\ProjectHasExtra;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ProjectHasExtraController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ProjectHasExtra(), function (Grid $grid) {
            $grid->model()->with(['project']);

            $grid->column('id')->sortable();
            $grid->column('enable')->switch();
            $grid->column('project.title');
            $grid->column('day_type')
                ->if(function () {
                    return $this->day_type === 0;
                })
                ->using(ProjectHasExtra::DAY_TYPE_OPTIONS)
                ->else()
                ->display(function () {
                    $start = $this->start_date?->format('Y-m-d');
                    $end = $this->end_date?->format('Y-m-d');
                    if ($start && ($start === $end))
                        return $start;
                    return "{$start} - {$end}";
                });
            $grid->column('range_data')->if(function () {
                return $this->time_type === 1;
            })
                ->label()
                ->else()
                ->display(function () {
                    return "全天";
                });
            $grid->column('extra_type')
                ->if(function () {
                    return $this->extra_type === 0;
                })
                ->using(ProjectHasExtra::EXTRA_TYPE_OPTIONS)
                ->else()
                ->display(function () {
                    return "修改预约量: $this->up";
                });


            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('project_id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ProjectHasExtra(), function (Show $show) {
            $show->field('id');
            $show->field('enable');
            $show->field('project_id');
            $show->field('day_type');
            $show->field('time_type');
            $show->field('extra_type');
            $show->field('start_date');
            $show->field('end_date');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('range_data');
            $show->field('up');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ProjectHasExtra(), function (Form $form) {
            $form->display('id');
            $form->switch('enable')->default(1);
            $form->select('project_id')
                ->options(Project::twoLevelOptions())
                ->load('range_data', admin_route('admin.api.project.time_range'));

            $form->radio('time_type')
                ->options(ProjectHasExtra::TIME_TYPE_OPTIONS)
                ->when(1, function (Form $form) {
                    $form->multipleSelect('range_data')->rules("required_if:time_type,1")->saveAsJson();

                })
                ->default(1)
                ->help('指定时段只会把全包含的时段禁止.例如有9:00-10:00 , 10:00-11:00两个时段,设置了9:30-11:00, 只会适用于10:00-11:00');
            $form->radio('day_type')
                ->options(ProjectHasExtra::DAY_TYPE_OPTIONS)
                ->when(1, function (Form $form) {
                    $form->dateRange('start_date', 'end_date', '生效日期')->rules("required_if:day_type,1");
                });
            $form->radio('extra_type')
                ->options(ProjectHasExtra::EXTRA_TYPE_OPTIONS)
                ->when(1, function ($form) {
                    $form->number('up');
                });
        });
    }
}
